import sqlite3

# 连接到数据库
try:
    conn = sqlite3.connect('backend/database.db')
    cursor = conn.cursor()
    
    # 查询orders表的结构
    cursor.execute("PRAGMA table_info(orders)")
    columns = cursor.fetchall()
    print("订单表结构:")
    for column in columns:
        print(column)
    
    # 查询订单数据，按用户ID 4过滤（与测试中使用的用户ID一致）
    cursor.execute("SELECT * FROM orders WHERE user_id = ? LIMIT 10", (4,))
    rows = cursor.fetchall()
    print("\n用户ID为4的订单数据:")
    if len(rows) == 0:
        print("没有找到用户ID为4的订单")
    else:
        for row in rows:
            print(row)
    
    # 查询所有订单，不考虑用户ID
    cursor.execute("SELECT * FROM orders LIMIT 10")
    all_rows = cursor.fetchall()
    print("\n所有订单数据 (前10条):")
    if len(all_rows) == 0:
        print("数据库中没有订单数据")
    else:
        for row in all_rows:
            print(row)
    
    # 查询订单状态分布
    cursor.execute("SELECT order_status, COUNT(*) FROM orders GROUP BY order_status")
    status_counts = cursor.fetchall()
    print("\n订单状态分布:")
    for status in status_counts:
        print(status)
    
    # 查询数据库中最大的订单ID
    cursor.execute("SELECT MAX(order_id) FROM orders")
    max_id = cursor.fetchone()[0]
    print(f"\n数据库中最大的订单ID: {max_id}")
    
    # 查询用户表，查看有哪些用户
    cursor.execute("SELECT user_id, username, role FROM users LIMIT 10")
    users = cursor.fetchall()
    print("\n用户数据 (前10条):")
    for user in users:
        print(user)
    
    # 查询订单详情表
    cursor.execute("SELECT * FROM order_details LIMIT 10")
    details = cursor.fetchall()
    print("\n订单详情数据 (前10条):")
    if len(details) == 0:
        print("数据库中没有订单详情数据")
    else:
        for detail in details:
            print(detail)
    
except sqlite3.Error as e:
    print(f"数据库错误: {e}")
except Exception as e:
    print(f"发生错误: {e}")
finally:
    if conn:
        conn.close()